
package logical_spaces.beans;

import Entity.DataFile;
import Entity.TableSpace;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import logical_spaces.business.LogicalSpaceBusiness;
import logical_spaces.business.TableSpaceBusiness;
import logical_spaces.business.TableSpaceDataModel;
import physical_spaces.business.DataFileDataModel;
import physical_spaces.business.PhysicalSpaceBusiness;
import login.beans.ConectionBean;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean
@SessionScoped

public class ModifyTableSpaceBeanPermanent implements Serializable {

    private List<TableSpace> tableSpaces;
    private TableSpaceDataModel mediumTableSpaceModel;
    private static final long serialVersionUID = 308L;
    
    //datafiles per tablespace
    private List<DataFile> dataFields ;
    private DataFileDataModel mediumDataFileModel;
    private DataFile selectedDataFile;
    
    private TableSpace selectedtablespace;
    private String nameTableSpace;
    private String newNameTableSpace;
    private String nameDataFile;
    private String sizeDataFile;
    private String dataFileSizeOption;
    private int index;
    //propiedades componentes
    private String disabled;
    private String disabledParent;
    private String modeOptionsDisabled;
    private String message;
    //editar
    private String online;
    private String mode;
    
    private Connection conn;

    public ModifyTableSpaceBeanPermanent(  ) {
        
        FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean =(ConectionBean) session.getAttribute("conectionBean");        
        this.conn = conectionBean.currentConnection;
        
        online = "true";
        mode = "true";
        message = "ocurrio un error";
        modeOptionsDisabled = "false";
        dataFileSizeOption = "K";
        
        tableSpaces = new ArrayList<>();
        chargeDataTableSpaces();
        mediumTableSpaceModel = new TableSpaceDataModel(tableSpaces);
       
        index = 0;
        disabled = "true";
        dataFields = new ArrayList<>();
                
    }

    public String getModeOptionsDisabled() {
        return modeOptionsDisabled;
    }

    public void setModeOptionsDisabled(String modeOptionsDisabled) {
        this.modeOptionsDisabled = modeOptionsDisabled;
    }

    public String getNewNameTableSpace() {
        return newNameTableSpace;
    }

    public void setNewNameTableSpace(String newNameTableSpace) {
        this.newNameTableSpace = newNameTableSpace;
    }

    public List<TableSpace> getTableSpaces() {
        return tableSpaces;
    }

    public void setTableSpaces(List<TableSpace> tableSpaces) {
        this.tableSpaces = tableSpaces;
    }

    public TableSpaceDataModel getMediumTableSpaceModel() {
        return mediumTableSpaceModel;
    }

    public void setMediumTableSpaceModel(TableSpaceDataModel mediumTableSpaceModel) {
        this.mediumTableSpaceModel = mediumTableSpaceModel;
    }

    public TableSpace getSelectedtablespace() {
        return selectedtablespace;
    }

    public void setSelectedtablespace(TableSpace selectedtablespace) {
        this.selectedtablespace = selectedtablespace;
    }

    public String getNameTableSpace() {
        return nameTableSpace;
    }

    public void setNameTableSpace(String nameTableSpace) {
        this.nameTableSpace = nameTableSpace;
    }

    public String getNameDataFile() {
        return nameDataFile;
    }

    public void setNameDataFile(String nameDataFile) {
        this.nameDataFile = nameDataFile;
    }

    public String getSizeDataFile() {
        return sizeDataFile;
    }

    public void setSizeDataFile(String sizeDataFile) {
        this.sizeDataFile = sizeDataFile;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getDisabled() {
        return disabled;
    }

    public void setDisabled(String disabled) {
        this.disabled = disabled;
    }

    public String getDisabledParent() {
        return disabledParent;
    }

    public void setDisabledParent(String disabledParent) {
        this.disabledParent = disabledParent;
    }

    public String getOnline() {
        return online;
    }

    public void setOnline(String online) {
        this.online = online;
    }

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<DataFile> getDataFields() {
        return dataFields;
    }

    public void setDataFields(List<DataFile> dataFields) {
        this.dataFields = dataFields;
    }

    public DataFileDataModel getMediumDataFileModel() {
        return mediumDataFileModel;
    }

    public void setMediumDataFileModel(DataFileDataModel mediumDataFileModel) {
        this.mediumDataFileModel = mediumDataFileModel;
    }

    public DataFile getSelectedDataFile() {
        return selectedDataFile;
    }

    public void setSelectedDataFile(DataFile selectedDataFile) {
        this.selectedDataFile = selectedDataFile;
    }   

    public String getDataFileSizeOption() {
        return dataFileSizeOption;
    }

    public void setDataFileSizeOption(String dataFileSizeOption) {
        this.dataFileSizeOption = dataFileSizeOption;
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }
    
    public void cancel(){
        index = 0;
        disabledParent = "false";
        disabled = "true";
    }
    
    public void updateModoDisabledState(){
        
        if("true".equals(online))
            modeOptionsDisabled = "false";
        if("false".equals(online))
            modeOptionsDisabled = "true";            
        
    }
    
    public void changeTab(){
        FacesContext context = FacesContext.getCurrentInstance();  
        if(selectedtablespace == null)
            context.addMessage(null,new FacesMessage("Error ","Por favor seleccione un Table Space"));
        else{
            nameTableSpace = selectedtablespace.getName();
            newNameTableSpace = nameTableSpace;
            disabledParent = "true";
            disabled = "false";
            index++;
            chargeDataDataFiles();
            mediumDataFileModel = new DataFileDataModel(dataFields);
        }    
    }

    private void chargeDataTableSpaces() {
        LogicalSpaceBusiness lsb;
        try {
            lsb = new LogicalSpaceBusiness( conn );
            tableSpaces = lsb.chargePermanentTableSpaceData();
        } catch (SQLException ex) {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN,
                            "Error de Consulta!",
                            "Please Try Again! " + ex.getMessage()));
        }
    }
    private void chargeDataDataFiles() {
        PhysicalSpaceBusiness psb = new PhysicalSpaceBusiness( conn );
        dataFields = psb.selectDataFilesPerTableSpace(nameTableSpace);
    }
    
    public void modifyTablespace() {

        FacesContext context = FacesContext.getCurrentInstance();

        TableSpaceBusiness tableSpaceBusinees = new TableSpaceBusiness( conn );
        if (online.equals("true")) {
            context.addMessage(null, new FacesMessage("Mensaje ", tableSpaceBusinees.crearQueryEstado(nameTableSpace, "ONLINE")));
            if (mode.equals("true")) {
                context.addMessage(null, new FacesMessage("Mensaje ", tableSpaceBusinees.crearQueryEstado(nameTableSpace, "READ ONLY")));
            } else {
                context.addMessage(null, new FacesMessage("Mensaje ", tableSpaceBusinees.crearQueryEstado(nameTableSpace, "READ WRITE")));
            }
        } else {
            context.addMessage(null, new FacesMessage("Mensaje ", tableSpaceBusinees.crearQueryEstado(nameTableSpace, "OFFLINE")));
        }      
        
        chargeDataDataFiles();
        mediumDataFileModel = new DataFileDataModel(dataFields);

    }
    
    public void addDataFile() {
        
        FacesContext context = FacesContext.getCurrentInstance();
        TableSpaceBusiness tableSpaceBusinees=new TableSpaceBusiness( conn );
        context.addMessage(null,new FacesMessage("Mensaje ",tableSpaceBusinees.crearQueryAgregarDataFile(nameTableSpace,nameDataFile,sizeDataFile,dataFileSizeOption)));
        chargeDataDataFiles();
        mediumDataFileModel = new DataFileDataModel(dataFields);
        
    }
    
    public void deleteDataFile() {
        
        FacesContext context = FacesContext.getCurrentInstance();
        TableSpaceBusiness tableSpaceBusinees = new TableSpaceBusiness( conn );
        if (selectedDataFile == null) {
            context.addMessage(null, new FacesMessage("Error ", "Por favor seleccione un DataFile"));
        } else {
            context.addMessage(null,new FacesMessage("Mensaje ",tableSpaceBusinees.crearQueryEliminarDataFilePerTableSpace(nameTableSpace,selectedDataFile.getName())));
            chargeDataDataFiles();
            mediumDataFileModel = new DataFileDataModel(dataFields);
        }
    }
    


}
